package okhttp3.internal.huc;

import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSocketFactory;

import okhttp3.Handshake;
import okhttp3.OkHttpClient;
import okhttp3.internal.URLFilter;

/**
 * <p>Title: OkHttpsURLConnection </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/11/21 16:34
 */
public final class OkHttpsURLConnection extends DelegatingHttpsURLConnection {
    private final OkHttpURLConnection delegate;

    public OkHttpsURLConnection(URL url, OkHttpClient client) {
        this(new OkHttpURLConnection(url, client));
    }

    public OkHttpsURLConnection(URL url, OkHttpClient client, URLFilter filter) {
        this(new OkHttpURLConnection(url, client, filter));
    }

    public OkHttpsURLConnection(OkHttpURLConnection delegate) {
        super(delegate);
        this.delegate = delegate;
    }

    protected Handshake handshake() {
        if (this.delegate.call == null) {
            throw new IllegalStateException("Connection has not yet been established");
        } else {
            return this.delegate.handshake;
        }
    }

    public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
        this.delegate.client = this.delegate.client.newBuilder().hostnameVerifier(hostnameVerifier).build();
    }

    public HostnameVerifier getHostnameVerifier() {
        return this.delegate.client.hostnameVerifier();
    }

    public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
        if (sslSocketFactory == null) {
            throw new IllegalArgumentException("sslSocketFactory == null");
        } else {
            this.delegate.client = this.delegate.client.newBuilder().sslSocketFactory(sslSocketFactory).build();
        }
    }

    public SSLSocketFactory getSSLSocketFactory() {
        return this.delegate.client.sslSocketFactory();
    }
}
